<template>
  <div class="main-content">
    <div class="search">
      <el-input placeholder="请输入订单编号" style="width: 200px" v-model="id"></el-input>
      <el-input placeholder="搜索订单" style="width: 200px" v-model="searchText"></el-input>
      <el-button type="info" plain style="margin-left: 10px" @click="loadOrders(1)">查询</el-button>
      <el-button type="warning" plain style="margin-left: 10px" @click="reset">重置</el-button>
    </div>
    <div style="width: 100%; background-color: white; margin: 30px auto; border-radius: 20px">
      <div style="padding-bottom: 10px">
        <div style="font-size: 18px; color: #000000FF; line-height: 80px; border-bottom: #cccccc 1px solid;">
          <div style="margin-left: 20px">我的订单（{{ ordersData.length }} 个）</div>
        </div>
        <div style="margin: 20px 0; padding: 0 50px">
          <div class="table">
            <el-table :data="ordersData" strip>
              <el-table-column label="商品图片" width="120px">
                <template v-slot="scope">
                  <el-image style="width: 80px; height: 60px; border-radius: 3px" v-if="scope.row.goodsImg"
                            :src="scope.row.goodsImg" :preview-src-list="[scope.row.goodsImg]"></el-image>
                </template>
              </el-table-column>
              <el-table-column prop="id" label="序号" width="80" align="center" sortable></el-table-column>
              <el-table-column prop="sn" label="订单编号"></el-table-column>
              <el-table-column prop="goodsName" label="商品名称" :show-overflow-tooltip="true">
                <template v-slot="scope">
                  <a @click="$router.push('/front/detail?id=' + scope.row.goodsId)">{{scope.row.goodsName}}</a>
                </template>
              </el-table-column>
              <el-table-column prop="businessName" label="店铺名称">
                <template v-slot="scope">
                  <a @click="$router.push('/front/business?id=' + scope.row.businessId)">{{scope.row.businessName}}</a>
                </template>
              </el-table-column>
              <el-table-column prop="goodsPrice" label="商品价格">
                <template v-slot="scope">
                  {{scope.row.goodsPrice}} / {{scope.row.unit}}
                </template>
              </el-table-column>
              <el-table-column prop="num" label="商品数量"></el-table-column>
              <el-table-column prop="price" label="订单总价"></el-table-column>
              <el-table-column prop="username" label="收货人"></el-table-column>
              <el-table-column prop="address" label="收货地址"></el-table-column>
              <el-table-column prop="addressPhone" label="联系电话"></el-table-column>
              <el-table-column prop="createTime" label="下单时间"></el-table-column>
              <el-table-column
                  prop="status"
                  label="订单状态"
                  show-overflow-tooltip
                  :formatter="formatOrderStatus"
              ></el-table-column>
              <el-table-column label="操作" align="center" width="210">
                <template v-slot="scope">
                  <el-button size="mini" type="primary"  v-if="scope.row.status === 0 "  plain @click="pay(scope.row)">支付</el-button>
                  <el-button size="mini" type="primary"  v-if="scope.row.status === 5 "  plain @click="alreadyGet(scope.row)">确认收货</el-button>
                  <el-button size="mini" type="primary"  v-if="scope.row.status === 6 "  plain @click="addComment(scope.row)" >评价</el-button>
                  <el-button size="mini" type="danger" plain @click="del(scope.row.id)">删除</el-button>
                  <el-button size="mini" type="primary"  v-if="scope.row.status === 0 " @click="handleEdit(scope.row)" plain >修改</el-button>
                  <el-button size="mini" type="primary"  v-if="scope.row.status === 1 "  plain >已支付 </el-button>
<!--                  v-if="scope.row.status === '待发货'"-->
                </template>
              </el-table-column>
            </el-table>

            <div class="pagination" style="margin-top: 20px">
              <el-pagination
                  background
                  @current-change="handleCurrentChange"
                  :current-page="current"
                  :page-sizes="[5, 10, 20]"
                  :page-size="pageSize"
                  layout="total, prev, pager, next"
                  :total="total">
              </el-pagination>
            </div>
          </div>
        </div>
      </div>
      <el-dialog title="请输入评价内容" :visible.sync="fromVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
        <el-form :model="form" label-width="100px" style="padding-right: 50px"  ref="formRef">
          <el-form-item label="评价内容" prop="username">
            <el-input type="textarea" v-model="form.content" placeholder="请输入评价内容"></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="fromVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveComment">确 定</el-button>
        </div>
      </el-dialog>


      <el-dialog title="信息" :visible.sync="fromVisible2" width="40%" :close-on-click-modal="false" destroy-on-close :show-close="false">
        <el-form label-width="100px" style="padding-right: 50px" :model="form"  ref="formRef">
          <el-form-item prop="num" label="商品数量">
            <el-input v-model="form.num" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="address" label="收货地址">
            <el-select v-model="addressId" placeholder="请选择收货地址" style="width: 100%">
              <el-option v-for="item in addressData" :label="item.username + ' - ' + item.userAddress + ' - ' + item.phone" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel" >取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </div>
      </el-dialog>

    </div>


  </div>
</template>

<script>
import {orderStatusEnum} from "@/enums/orderStatus";
export default {

  data() {
    return {
      orderStatusEnum: orderStatusEnum,
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      ordersData: [],
      current: 1,   // 当前的页码
      pageSize: 10,  // 每页显示的个数
      total: 0,
      id:null,
      fromVisible: false,
      fromVisible2: false,
      addressId: null,
      searchText:null,
      addressData: [],
      form: {},
    }
  },
  mounted() {
    this.loadOrders(1)
    this.loadAddress()
  },
  // methods：本页面所有的点击事件或者其他函数定义区
  methods: {
    formatOrderStatus(row, column, cellValue, index) {
      // 使用 orderStatusEnum 获取对应的描述
      return this.orderStatusEnum[cellValue] || '未知状态';
    },
    loadOrders(current) {
      if (current) this.current = current
      this.$request.post('/orders/selectPage/my', {

          current: this.current,
          pageSize: this.pageSize,
         goodsName:this.goodsName,
           address: this.address,
        addressPhone: this.addressPhone,
        businessName: this.businessName,
        status:this.status,
        username:this.username,
        id:this.id,
        sn:this.sn,
        searchText:this.searchText
      }).then(res => {
        if (res.code === 0) {
          console.log(res)
          this.ordersData = res.data?.records
          this.total = res.data?.total
        } else {
          this.$message.error(res.message)
        }
      })
    },
    loadAddress() {
      this.$request.get('/address/selectAll').then(res => {
        if (res.code === 0) {
          this.addressData = res.data
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleEdit(row) {   // 编辑数据
      this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.fromVisible2 = true   // 打开弹窗
    },
    del(id) {
      this.$request.delete('/orders/delete/' + id).then(res => {
        if (res.code === 0) {
          this.$message.success('删除成功')
          this.loadOrders(1)
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleCurrentChange(current) {
      this.loadOrders(current)
    },
    pay(row) {
      this.form = row
      this.$request.get('/orders/pay',{
        params:{
          ordersId:this.form.id
        }

      }).then(res => {
        if (res.code === 0) {
          this.$message.success('操作成功')
          this.loadOrders(1)
        } else {
          this.$message.error(res.message)
        }
      })
    },

    alreadyGet(row){
      this.form = row
      this.$request.post('/orders/alreadyGetGood',{
        addressId:this.form.addressId,
        goodsId:this.form.goodsId,
        num:this.form.num,
        id:this.form.id


      }).then(res => {
        if (res.code === 0) {
          this.$message.success('操作成功')
          this.loadOrders(1)
        } else {
          this.$message.error(res.message)
        }
      })
    },
    cancel() {
      this.fromVisible2 = false
    },

    save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
            this.$request.post('/orders/update', {
              num:this.form.num,
              addressId:this.addressId,
              id:this.form.id,
              goodsId:this.form.goodsId,

            }).then(res => {
              if (res.code === 0) {  // 表示成功保存
                this.$message.success('保存成功')
                this.loadOrders(1)
                this.fromVisible2 = false
              } else {
                this.$message.error(res.message)  // 弹出错误的信息
              }
            })


    },

    addComment(row) {
      this.fromVisible = true
      this.form = row
    },
    reset() {
      this.id = null,
        this.searchText=null
      this.loadOrders(1)
    },

    saveComment() {
      let data = {
        userId: this.user.id,
        businessId: this.form.businessId,
        goodsId: this.form.goodsId,
        content: this.form.content,
      }
      this.$request.post('/comment/add', data).then(res => {
        if (res.code === 0) {
          this.$message.success('评价成功')
          this.fromVisible = false
          //this.updateStatus(this.form, '已评价')
          this.form = {}
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>
